package com.aaron.kjviewutils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aaron.kjviewutils.flight.FlightAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/1.
 */

public class DropDownMenuAdapter extends RecyclerView.Adapter<DropDownMenuAdapter.FilterViewHolder> {

    private List<String> datas;
    private Context context;

    private FlightAdapter.OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(FlightAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public DropDownMenuAdapter(Context context, List<String> datas)
    {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public DropDownMenuAdapter.FilterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_carte, parent, false);
        return new DropDownMenuAdapter.FilterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final DropDownMenuAdapter.FilterViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty()&&null!=datas.get(position))
        {
            String data = datas.get(position);
            holder.flightItemTag.setText(data);
            if(null!=mOnItemClickListener)
            {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }


    public static class FilterViewHolder extends RecyclerView.ViewHolder
    {

        @Bind(R.id.carteItemTag)
        TextView flightItemTag;

        public FilterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * item点击事件
     */
    public interface OnItemClickListener {
        /**
         * item点击事件
         *
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}
